const GET_CURRENT_ADDRESS = 'get_current_address'
const SET_CURRENT_ADDRESS = 'set_current_address'
const SET_EDITTING_ADDRESS = 'set_editting_address' //点击编辑当前的地址
const GET_ADDRESSES = 'get_addresses' //初始化地址列表
const NEW_ADDRESS = 'new_address'
const EDIT_ADDRESS = 'edit_address' //编辑当前地址
const DELETE_ADDRESS = 'delate_address' //删除当前地址
const DEFAULT_ADDRESS = 'default_address' //设置为默认地址
const EDIT_ADDRESS_INIT = "edit_address_init" //点击新增收货地址按钮初始化地址
const PUSH_NEW_ADDRESS = "push_new_address" //点击新增收货地址里面的保存


const state = {
    currentAddress: null, //获取默认的地址
    edittingAddress: {}, //编辑地址
    addressList: [], //地址列表
}

const getters = {
    currentAddress: (state) => state.currentAddress,
    edittingAddress: (state) => state.edittingAddress,
    addressList: (state) => state.addressList
}

const mutations = {
    [GET_ADDRESSES](state, addressList) { //初始化地址列表
        state.addressList = addressList
    },
    [DEFAULT_ADDRESS](state, addressItemId) { //设置为默认，变为false
        for (let i = 0; i < state.addressList.length; i++) {
            if (state.addressList[i].id === addressItemId) {
                state.addressList[i].isDefault = true
            } else {
                state.addressList[i].isDefault = false
            }
        }
    },
    [SET_CURRENT_ADDRESS](state, address) { //设置默认地址
        state.currentAddress = address
    },
    [SET_EDITTING_ADDRESS](state, address) { //点击编辑当前的地址
        state.edittingAddress = address
    },
    [EDIT_ADDRESS](state, address) {
        for (let i = 0; i < state.addressList.length; i++) {
            if (state.addressList[i].id === address.id) {
                state.addressList.splice(i, 1, address)
                break
            }
        }
    },
    [DELETE_ADDRESS](state, address) { //删除当前地址
        if (state.currentAddress != null && state.currentAddress.id === address.id) {
            state.currentAddress = null
        }
        for (let i = 0; i < state.addressList.length; i++) {
            if (state.addressList[i].id === address.id) {
                state.addressList.splice(i, 1)
                break
            }
        }
    },

    [EDIT_ADDRESS_INIT](state) { //点击新增收货地址按钮初始化地址
        state.edittingAddress = {}
    },
    [PUSH_NEW_ADDRESS](state, address) { //点击新增收货地址里面的保存
        state.addressList.push(address)
    },
}

const actions = {
    getAddresses({ commit }, address) { //初始化地址列表
        commit(GET_ADDRESSES, address)
    },
    defaultAddress({ commit }, addressItemId) { //设置为默认，变为false
        commit(DEFAULT_ADDRESS, addressItemId)
    },
    setCurrentAddress({ commit }, address) { //设置默认地址
        commit(SET_CURRENT_ADDRESS, address)
    },
    editAddress({ commit }, address) { //点击编辑当前的地址
        commit(SET_EDITTING_ADDRESS, address)
    },
    updateAddress({ commit }, address) { //更新当前地址
        commit(EDIT_ADDRESS, address)
    },
    deleteAddress({ commit }, address) { //删除当前地址
        commit(DELETE_ADDRESS, address)
    },
    editAddressInit({ commit }) { //点击新增收货地址按钮初始化地址
        commit(EDIT_ADDRESS_INIT)
    },
    pushNewAddress({ commit }, address) { //点击新增收货地址里面的保存
        commit(PUSH_NEW_ADDRESS, address)
    },
}

export default {
    state,
    getters,
    mutations,
    actions
}